package seleniumdemo.base;

import com.google.common.collect.ImmutableList;
import com.vaadin.testbench.TestBench;
import com.vaadin.testbench.TestBenchTestCase;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.rules.ExternalResource;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import seleniumdemo.util.PropertyLoader;

import java.io.File;
import java.util.concurrent.TimeUnit;

/**
* Base class for all the JUnit-based test classes
*/
public class JUnitTestBase extends TestBenchTestCase {

  protected static String gridHubUrl;
  protected static String baseUrl;
  protected static Capabilities capabilities;

  protected WebDriver driver;

  @ClassRule
  public static ExternalResource webDriverProperties = new ExternalResource() {
    @Override
    protected void before() throws Throwable {
      baseUrl = PropertyLoader.loadProperty("site.url");
      gridHubUrl = PropertyLoader.loadProperty("grid2.hub");
      baseUrl = PropertyLoader.loadProperty("site.url");
      gridHubUrl = PropertyLoader.loadProperty("grid.url");
      if ("".equals(gridHubUrl)) {
        gridHubUrl = null;
      }
      capabilities = PropertyLoader.loadCapabilities();
    };
  };

  @Rule
  public ExternalResource webDriver = new ExternalResource() {
    @Override
    protected void before() throws Throwable {
      System.setProperty("vaadin.testbench.developer.license", "1");
      System.setProperty("webdriver.chrome.driver", "C:/opt/chromedriver.exe");
      //driver = WebDriverFactory.getDriver(gridHubUrl, capabilities);
       //TestBench.createDriver(new ChromeDriver(capabilities));
        ChromeDriverService src = new ChromeDriverService.Builder().usingDriverExecutable(new File("C:/opt/chromedriver.exe")).usingAnyFreePort().build();
        src.start();
      driver = TestBench.createDriver(new ChromeDriver(src));}

      protected void after() {
           driver.close();
      };
  };
}
